package com.parkinglot.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.parkinglot.core.Result;
import com.parkinglot.parmeter.*;
import com.parkinglot.pojo.Order;
import com.parkinglot.pojo.User;
import com.parkinglot.pojo.UserCar;
import com.parkinglot.service.*;
import com.parkinglot.util.CarNumberInit;
import com.parkinglot.util.HttpUrlutils;
import com.parkinglot.vo.MonthbillUserRecordVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("userserver")
@Api(tags = "用户接口")
@Slf4j
public class UserController {

    @Resource
    private UserService userService;
    @Resource
    private GaragePayConfigService garagePayConfigService;
    @Resource
    private MonthbilRecordService monthbilRecordService;
    @Resource
    private OrderService orderService;
    @Resource
    private UserCarService userCarService;

    @Resource
    private HttpUrlutils httpUrlutils;

    @ApiOperation(value = "查询所有用户")
    @GetMapping("/getalluser")
    public Result getalluser(){
        return Result.success(userService.getalluser());
    }

    @ApiOperation(value = "新增用户")
    @PostMapping("/adduser")
    public Result addUser( UserParm userParm){
        return Result.success(userService.insert(userParm));
    }

    @ApiOperation(value = "车库支付配置")
    @PostMapping("/addgaragepayconfig")
    public Result addgaragepayconfig(@RequestBody GaragePayConfigaddParm garagePayConfigaddParm){
        return Result.success(garagePayConfigService.insert(garagePayConfigaddParm));
    }

  /*  @ApiOperation(value = "用户新增趋势昨日(目前按照每小时统计)")
    @GetMapping("/userzrcount")
    public Result userzrcount(){
        return Result.success(userService.userzrcount());
    }

    @ApiOperation(value = "所有用户或某个车库下所有用户上周新增趋势(目前按照每天统计)")
    @GetMapping("/userweekcount")
    public Result userweekcount(String gardecode){
        return Result.success(userService.userweekcount(gardecode));
    }
*/
  /*  @ApiOperation(value = "所有用户或某个车库下所有用户上个月新增趋势")
    @GetMapping("/usermonthcount")
    public Result usermonthcount(String gardecode){
        return Result.success(userService.usermonthcount(gardecode));
    }
*/
    @ApiOperation(value = "月票用户充值记录")
    @GetMapping("/userrechargerecord")
    public Result userrechargerecord(String gardecode,int page,int limit){
        PageHelper.startPage(page, limit);
        List<MonthbillUserRecordVo>list=monthbilRecordService.userrechargerecord(gardecode);
        return Result.success(new PageInfo<>(list));
    }

    @ApiOperation(value = "月票用户退卡记录")
    @GetMapping("/userrefundrecord")
    public Result userrefundrecord(String gardecode,int page,int limit){
        PageHelper.startPage(page, limit);
        List<MonthbillUserRecordVo>list=monthbilRecordService.userrefundrecord(gardecode);
        return Result.success(new PageInfo<>(list));
    }


    @ApiOperation(value = "")
    @PostMapping("/trueingarage")
    public Result userrefundrecord(@RequestBody GarageUser garageUser){
        return Result.success(userService.queryUserByid(garageUser));
    }




    @ApiOperation(value = "登录")
    @GetMapping("/userlogin")
    public Result Login(String name,String password){
       String zh="admin";
       String pass="123456";
       if(name .equals(zh)&&password.equals(pass)){
           return  Result.success("登录成功");
       }
        return  Result.success("登录失败");


    }
    @ApiOperation(value = "扫码进入停车场 随机生成车牌号")
    @GetMapping("/sccarnumber")
    public Result useringarage(){

        return Result.success(CarNumberInit.carinit());
    }
    /**
     * 用户点击确认入场
     * 绑定车牌
     * 生成订单
     * 中间件抬杆
     * @param garageUser
     * @return
     */
    @ApiOperation(value = "点击确认入场 绑定车牌 生成订单")
    @PostMapping("/bindcar")
    public Result bindcar(@RequestBody GarageUser garageUser) throws Exception {
        return  Result.success(userService.BindCarNumber( garageUser));
    }

    /**
     *用户点击确定出场
     * 修改订单出场时间
     * 并计算金额 修改订单金额
     * @param
     * @param
     * @return
     */
    @ApiOperation(value = "点击确认出场  修改订单 并计算金额")
    @PostMapping("/caroutgarage")
    public Result caroutgarage(@RequestBody Order order){
        return  Result.success(orderService.insert( order));
    }

    @ApiOperation(value = "绑定手机号")
    @PostMapping("/bindphone")
    public Result bindphone(String userid,String phone){
        User user=new User();
      user.setUserid(userid);
        user.setPhone(phone);
        return  Result.success(userService.update( user));
    }

    @PostMapping("/getopenid")
    @ApiOperation("获取用户openid")
    public Result getopenid(@RequestBody WeChatParm weChatParm) {
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid=wx9832ac19576d1e1b&secret=fb83c553d05fd30d1a62c94c879596f4&js_code=" + weChatParm.getJscode() + "&grant_type=authorization_code";
        Map<String, Object> map = new HashMap<String, Object>();
        try {
           map=httpUrlutils.getresultty(url);
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.success(map);
    }
    @ApiOperation(value = "月卡绑定车牌")
    @PostMapping("/monthbindcar")
    public Result monthbindcar(@RequestBody GarageUser garageUser){
        return  Result.success(userService.monthBindCarNumber( garageUser));
    }


    @ApiOperation(value = "小程序查询用户绑定的车是否是会员")
    @PostMapping("/getusercarmonthvip")
    public Result getusercarmonthvip(@RequestBody UserCarMonthvip userCarMonthvip){
        return  Result.success(userService.getusercarmonthvip( userCarMonthvip));
    }



    //  getMaxParkingTimeByUserid
    @ApiOperation(value = "查询订单表用户最近的停车订单记录  最大的时间")
    @GetMapping("/getmaxparkingtime")
    public Result getMaxParkingTimeByUserid(String garageid,String userid,String carnumber){
        return  Result.success(orderService.getMaxParkingTimeByUserid(garageid, userid,carnumber));
    }

    /**
     * 有牌车入场 扫了预付款二维码 中间件给的车牌号 需要用户去绑定车牌
     * 无牌车进入停车场 扫了预付款  返回给前端车牌信息 不做任何新增操作
     * @param haveCarnumber
     * @return
     */
    @ApiOperation(value = " 有牌车入场 扫了预付款二维码 中间件给的车牌号 需要用户去绑定车牌")
    @GetMapping("/havacarbind")
    public Result havacarbind(HaveCarnumber haveCarnumber){
        return  Result.success(userService.havecarnumber(haveCarnumber));
    }


    @ApiOperation(value = " 去支付 查询设置订单的出场时间 并计算金额")
    @PostMapping("/wxgopay")
    public Result wxgopay(@RequestBody OrderPayParm orderPayParm ){
        return  Result.success(orderService.wxgopay(orderPayParm));
    }


    //getusercarpaysattus
    @ApiOperation(value = " 小程序用户绑定的车辆")
    @PostMapping("/getusercarpaysattus")
    public Result getusercarpaysattus( String garageid,String userid ){
        return  Result.success(userService.getusercarpaysattus(garageid,userid));
    }
    //getyufukuanorder
    @ApiOperation(value = " 预付款支付已付订单列表")
    @GetMapping("/getyufukuanorder")
    public Result getyufukuanorder( String garageid,String userid ){
        return  Result.success(orderService.getyufukuanorder(garageid,userid));
    }

    @ApiOperation(value = "小程序删除车牌号")
    @PostMapping("/updatecarnumber")
    public Result updatecarnumber(@RequestBody UserCar userCar){

        return  Result.success(userCarService.updateusercar(userCar));
    }
}
